﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using CoTime.Model;
using CoTime.View.UserControls;
using CoTime.ViewModel;

namespace CoTime.View
{
    /// <summary>
    /// Interaction logic for UCAI.xaml
    /// </summary>
    public partial class UCAi : UserControl
    {
        #region [ Fields & Properties ]

        private MainWindowViewModel viewModel;

        #endregion

        #region [ Constructors ]

        public UCAi()
        {
            InitializeComponent();
        }

        public UCAi(MainWindowViewModel viewModel) : this()
        {
            this.viewModel = viewModel;

            InitUI();
        }

        #endregion

        #region [ Init ]

        void InitUI()
        {
            List<Note> notes = viewModel.Notes;

            String[] lines = File.ReadAllLines(Path.Combine(Config.AppPath, "Images/Notes/2.txt"));
            Int32 i = 0;
            foreach (String line in lines)
            {
                if (!String.IsNullOrWhiteSpace(line))
                {
                    //52,25 ;400,25 ;52,236 ;400,236
                    String[] coordinateArr = line.Split(new char[] { ';', ',' });
                    Double[] coordinates = Array.ConvertAll<String, Double>(coordinateArr, delegate(String s)
                    {
                        return Double.Parse(s);
                    });
                    if (coordinates.Length == 8)
                    {
                        Double noteHeight = coordinates[5] - coordinates[3];
                        Double noteWidth = coordinates[2] - coordinates[0];
                        Int32 linesCount = Convert.ToInt32(noteHeight / 30);

                        UCNote note = new UCNote(notes.GetNoteById(i++), linesCount, noteWidth);

                        note.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                        note.VerticalAlignment = System.Windows.VerticalAlignment.Top;

                        note.Width = noteWidth;
                        note.Height = noteHeight;

                        note.Margin = new Thickness(coordinates[0], coordinates[1] - 30, 0, 0);
                        note.Background = Brushes.Transparent;
                        UIAiGrid.Children.Add(note);
                    }
                    else
                    {
                        MessageBox.Show("坐标配置文件不正确，帮助请按F2");
                    }
                }
            }
        }

        #endregion

        #region [ UI Helper ]

        public void SetUIBackground()
        {
            String[] files = Directory.GetFiles(Config.AppPath + "Images/Notes", "2.??g");
            if (files.Length > 0)
            {
                Random random = new Random();
                Int32 randomIndex = random.Next(0, files.Length);
                ImageBrush imageBrush = new ImageBrush();
                imageBrush.Opacity = .65;
                BitmapImage bitmap = new BitmapImage(new Uri(files[randomIndex]));
                this.Width = bitmap.Width;
                this.Height = bitmap.Height;
                imageBrush.ImageSource = bitmap;
                imageBrush.Stretch = Stretch.UniformToFill;
                imageBrush.AlignmentX = AlignmentX.Left;
                imageBrush.AlignmentY = AlignmentY.Top;
                this.Background = imageBrush;
            }
        }

        #endregion

        #region [ UI To Model Mappers ]

        public List<Note> NotesMapper()
        {
            List<Note> notes = new List<Note>();

            UIAiGrid.Dispatcher.Invoke(new Action(() =>
            {
                var ucNotes = UIAiGrid.FindAll<UCNote>();
                foreach (var ucNote in ucNotes)
                {
                    notes.Add(ucNote.ToNote());
                }
            }));

            return notes;
        }

        #endregion

        #region [ Api ]

        public void ClearDone()
        {
            var notes = this.UIAiGrid.FindAll<UCNote>();
            if (notes != null && notes.Count > 0)
            {
                notes.ForEach(note => note.ClearDone());
            }
        }

        public void SyncBackToNote(Task task)
        {
            var notes = GetAllNotes();
            if (notes != null && notes.Count > 0)
            {
                notes.ForEach(note => note.UpdateTaskDoneStatus(task));
            }
        }

        private List<UCNote> GetAllNotes()
        {
            return this.UIAiGrid.FindAll<UCNote>();
        }

        #endregion
    }
}
